package cn.wolfcode.car.business.service.impl;
import cn.wolfcode.car.business.domain.CustomerLinkMane;
import cn.wolfcode.car.business.mapper.CustomerLinkManeMapper;
import cn.wolfcode.car.business.query.CustomerLinkManeQuery;
import cn.wolfcode.car.business.service.ICustomerLinkManeService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CustomerLinkManeServiceImpl implements ICustomerLinkManeService {

    @Autowired
    private CustomerLinkManeMapper customerLinkManeMapper;


    @Override
    public TablePageInfo<CustomerLinkMane> query(CustomerLinkManeQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<CustomerLinkMane>(customerLinkManeMapper.selectForList(qo));
    }


    @Override
    public void save(CustomerLinkMane customerLinkMane) {
        if (customerLinkMane.getCustomerId() == null){
            throw new BusinessException("请选择所属客户");
        }
        customerLinkMane.setInputTime(new Date());
        customerLinkMane.setInputUser(ShiroUtils.getUser().getId());
        customerLinkManeMapper.insert(customerLinkMane);
    }

    @Override
    public CustomerLinkMane get(Long id) {
        return customerLinkManeMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(CustomerLinkMane customerLinkMane) {
        if (customerLinkMane.getCustomerId() == null){
            throw new BusinessException("请选择所属客户");
        }
        customerLinkManeMapper.updateByPrimaryKey(customerLinkMane);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            customerLinkManeMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<CustomerLinkMane> list() {
        return customerLinkManeMapper.selectAll();
    }
}
